package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.util.ZipUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBorrowerFlowInformation;
import cn.stylefeng.guns.sys.modular.system.entity.GcgWsjd;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.guns.sys.modular.system.model.params.GcgWsjdParam;
import cn.stylefeng.guns.sys.modular.system.service.GcgWsjdTakeawayService;
import cn.stylefeng.guns.sys.modular.system.warpper.MenuWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 网上建店
 *
 * @author liuy
 * @Date 2021-05-18 13:30:40
 */
@Controller
@RequestMapping("/gcgWsjdTakeaway")
public class GcgWsjdTakeawayController extends BaseController {
    private String PREFIX = "/modular/system/projectwsjd";

    @Autowired
    private GcgWsjdTakeawayService gcgWsjdTakeawayService;

    /**
     * 跳转到外卖页面
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("")
    public String index(ModelMap mm) {
        Object[] objects = LoginContextHolder.getContext().getUser().getPermissions().toArray();
        int a = 1;
        for (Object sd:objects
        ) {
            if("/btyWsjd/edit".equals(sd.toString())){
                a=2;
            }
        }
        mm.put("a",a);
        return PREFIX + "/projectwsjd_waimai.html";
    }

    /**
     * 跳转到外卖区域页面
     *
     */
    @RequestMapping("/wmqy")
    public String indexQy(ModelMap mm) {
        Object[] objects = LoginContextHolder.getContext().getUser().getPermissions().toArray();
        int a = 1;
        for (Object sd : objects) {
            if("/btyWsjd/edit".equals(sd.toString())){
                a=2;
            }
        }
        mm.put("a",a);
        return PREFIX + "/projectwsjd_wmqy.html";
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(GcgWsjdParam gcgWsjdParam) {
        return this.gcgWsjdTakeawayService.TakeawayList(gcgWsjdParam);
    }

    /**
     * 查询列表（区域）
     */
    @ResponseBody
    @RequestMapping("/qyList")
    public LayuiPageInfo qyList(GcgWsjdParam gcgWsjdParam) {
        return this.gcgWsjdTakeawayService.qyList(gcgWsjdParam);
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GcgWsjd gcgWsjd) {
        if (ToolUtil.isOneEmpty(gcgWsjd, gcgWsjd.getGcgWsjdId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        gcgWsjd.setUpdateTime(new Date());
        gcgWsjd.setUpdateUser(LoginContextHolder.getContext().getUserId());
        this.gcgWsjdTakeawayService.updateDmById(gcgWsjd);
        return SUCCESS_TIP;
    }

    /**
     * 导出excel列表
     */
    @ResponseBody
    @RequestMapping("/listExport")
    public void listExport(Integer taStatus,HttpServletResponse response, CwbbParam cwbbParam) {
         this.gcgWsjdTakeawayService.ExportList(taStatus,response,cwbbParam);
    }

    /**
     * 导出excel列表
     */
    @ResponseBody
    @RequestMapping("/listExportQy")
    public void listExportQy(Integer taStatus,HttpServletResponse response, CwbbParam cwbbParam) {
        this.gcgWsjdTakeawayService.listExportQy(taStatus,response,cwbbParam);
    }

    /**
     * 批量完成任务
     */
    @RequestMapping(value = "/doneSome")
    @ResponseBody
    public Object update(@RequestParam("ids") String ids, @RequestParam("taStatus") Integer taStatus) {
        return this.gcgWsjdTakeawayService.update(ids, taStatus);
//        return SUCCESS_TIP;
    }

    /**
     * 批量下载图片
     */
    @RequestMapping(value = "/btnDown")
    @ResponseBody
    public Object btnDown(@RequestParam("ids") String ids, HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.gcgWsjdTakeawayService.btnDown(ids,request,response);
        String urls="D:/wsjd/csh/wmtp/";
        ZipUtil.zip(urls);
        return SUCCESS_TIP;
    }

    /**
     * 通过
     */
    @RequestMapping(value = "/pass")
    @ResponseBody
    public Object pass(@RequestParam("id") Long id) {
        this.gcgWsjdTakeawayService.pass(id);
        return SUCCESS_TIP;
    }

    /**
     * 批量通过
     */
    @RequestMapping(value = "/updatePassMany")
    @ResponseBody
    public Object updatePassMany(@RequestParam("id") String id) {
        this.gcgWsjdTakeawayService.updatePassMany(id);
        return SUCCESS_TIP;
    }

    /**
     * 驳回页面
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/rebut")
    public String edit(@RequestParam Long gcgWsjdId, Model model) {
        model.addAttribute("gcgWsjdId", gcgWsjdId);
        return PREFIX + "/projectwsjd_rebut.html";
    }

    /**
     * 驳回
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @ResponseBody
    @RequestMapping(value = "/rebutL")
    public Object rebutL(@RequestParam("id") Long id, @RequestParam(value = "data") String data) {
        this.gcgWsjdTakeawayService.rebutL(id, data);
        return SUCCESS_TIP;
    }

    /**
     * 驳回项树
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping(value = "/listTree")
    @ResponseBody
    public Object listTree() {
        List<Map<String, Object>> menus = this.gcgWsjdTakeawayService.selectMenuTree();
        List<Map<String, Object>> menusWrap = new MenuWrapper(menus).wrap();
        LayuiPageInfo result = new LayuiPageInfo();
        result.setData(menusWrap);
        return result;
    }

}
